package edu.sctu.renpingan.url;

import com.sun.net.ssl.HttpsURLConnection;
import edu.sctu.xialei.june.day20150610one.modle.SafetyInspector;
import sun.net.www.protocol.http.HttpURLConnection;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;

/**
 * Created by Administrator on 2015/6/16.
 */
public class UrlAndUrlconnection {
    public  void getUrls(){
        int count;
        String path = "http://www.baidu.com";
        byte[] buffer = new byte[1024];
        try {
           URL url = new URL(path);
            try {
                InputStream inputStream = url.openStream();

                while((count = inputStream.read(buffer))!= -1){
                    System.out.println(new String(buffer,0,count,"UTF-8"));
                } inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }


        public  void getUrls1(){
            int count;
            String path = "http://www.baidu.com";
            byte[] buffer = new byte[1024];
            try {
                URL url = new URL(path);
                try {
                    URLConnection urli = url.openConnection();
                    urli.connect();
                    InputStream inputStream = urli.getInputStream();

                    while((count = inputStream.read(buffer))!= -1){
                        System.out.println(new String(buffer,0,count,"UTF-8"));
                    } inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }

    public  void getUrls2(){
        int count;
        String path = "http://www.baidu.com";
        byte[] buffer = new byte[1024];
        try {
            URL url = new URL(path);
            try {
                HttpURLConnection urli = (HttpURLConnection) url.openConnection();
                urli.connect();
                InputStream inputStream = urli.getInputStream();

                while((count = inputStream.read(buffer))!= -1){
                    System.out.println(new String(buffer,0,count,"UTF-8"));
                } inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    /**
     * post请求
     * @param string
     * @throws IOException
     */
    public void getPost(String string) throws IOException {


        byte[] buffers = new byte[5000];

        URL url = new URL(Final.URL_CONECTION);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.connect();
        httpURLConnection.setDoInput(true);
        httpURLConnection.setDoOutput(true);
        OutputStream stream = httpURLConnection.getOutputStream();
        stream.flush();
        stream.write(Integer.parseInt(string));


        InputStream inputStream = httpURLConnection.getInputStream();
        int number;
        while (( number = inputStream.read(buffers))!=-1){

            System.out.println(new String(buffers,0,number,"Utf-8"));

        }



    }



}

